﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeviceUnion.VLC
{
    public partial class VLC_CaptureDesktop : Form
    {
        public VLC_CaptureDesktop()
        {
            InitializeComponent();
            this.Tag = this.Text + " - {0}";
            Init();
        }

        private void Init()
        {
            this.vlcControl1 = new Vlc.DotNet.Forms.VlcControl();
            // 
            // vlcControl1
            // 
            this.vlcControl1.BackColor = System.Drawing.Color.Black;
            this.vlcControl1.Location = new System.Drawing.Point(2, 2);
            this.vlcControl1.Name = "vlcControl1";
            this.vlcControl1.Size = new System.Drawing.Size(649, 600);
            this.vlcControl1.Spu = -1;
            this.vlcControl1.TabIndex = 0;
            this.vlcControl1.Text = "vlcControl1";
            this.vlcControl1.BeginInit();
            this.vlcControl1.VlcLibDirectory = new System.IO.DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory + "\\vlc");
            this.vlcControl1.VlcMediaplayerOptions = new[] { "--file-logging", "-vvv", "--logfile=Logs.log" };
            this.vlcControl1.EndInit();
            this.vlcControl1.Video.FullScreen = true;
            this.vlcControl1.Dock = DockStyle.Fill;
            this.panel1_vlc.Controls.Add(this.vlcControl1);
            this.FormClosing += CaptureDesktop_FormClosing;
        }

        private void CaptureDesktop_FormClosing(object sender, FormClosingEventArgs e)
        {
            vlcControl1.Stop();
        }

        /// <summary>
        /// play
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            var btn = sender as Button;
            switch (btn.Text.ToLower())
            {
                case "play":
                    {
                        var f = new OpenFileDialog();
                        f.Multiselect = false;
                        if (f.ShowDialog() == DialogResult.OK)
                        {
                            this.vlcControl1.Stop();
                            var fi = f.FileName;
                            this.vlcControl1.SetMedia(new System.IO.FileInfo(fi));
                            this.vlcControl1.Play();
                        }
                    }
                    break;
                case "pause":
                    {
                        vlcControl1.Pause();
                        btn.Text = "Continue";
                    }
                    break;
                case "continue":
                    {
                        vlcControl1.Play();
                        btn.Text = "Pause";
                    }
                    break;
                case "stop":
                    {
                        vlcControl1.Stop();
                    }
                    break;
                default:
                    break;
            }

        }

        private void Button4_Click(object sender, EventArgs e)
        {
            if (vlcControl1.GetCurrentMedia()==null|| vlcControl1.GetCurrentMedia().URL != "screen://")
            {
                this.vlcControl1.Stop();
                var options = new string[]
                {
                    ":screen-fps=25.000000",
                    //":live-caching=300",
                    ":screen-mouse-image=16.png",
                    ":sout=#duplicate{dst=std{access=file,mux=ts,dst='.\\006.ts'},dst=display}}",
            };
                this.vlcControl1.SetMedia("screen://", options);
                this.vlcControl1.Play();
            }
        }
    }
}
